package com.example.banksystem.service;

import com.example.banksystem.dao.UserRepository;
import com.example.banksystem.entity.Account;
import com.example.banksystem.entity.User;
import com.example.banksystem.repository.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private AccountRepository accountRepository;

    public String register(User user) {
        if (userRepository.findByUsername(user.getUsername()) != null) {
            return "用户名已存在！";
        }
        userRepository.save(user);
        return "注册成功！";
    }

    public User login(String username, String password) {
        // 直接调用Repository的方法进行用户名和密码验证
        return userRepository.findByUsernameAndPassword(username, password);
    }


    // 根据用户ID获取用户信息
    public User getUserById(Long id) {
        return userRepository.findById(id).orElse(null);
    }

    // 根据用户ID获取账户信息
    public Account getAccountByUserId(Long userId) {
        return accountRepository.findByUserId(userId);
    }

}
